package bulksms.com;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BulksmsActivity extends Activity {
	Button sms;
	EditText Phone;
	EditText content;
	EditText times;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Phone = (EditText) findViewById(R.id.editText1);
		content = (EditText) findViewById(R.id.editText2);
		sms = (Button) findViewById(R.id.button1);
		times = (EditText) findViewById(R.id.editText3);

		sms.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sms1 = content.getText().toString();
				String phone1 = Phone.getText().toString();
				String times1 = times.getText().toString();

				try {

					if (phone1.equalsIgnoreCase("")
							|| (phone1.trim().length() <10))
					{
						Toast.makeText(getBaseContext(), "Invalid Number ???",
								Toast.LENGTH_LONG).show();
							

					} else if (sms1.equalsIgnoreCase("")
							|| sms1.trim().length() == 0) {
						Toast.makeText(getBaseContext(), "Content ???",
								Toast.LENGTH_LONG).show();

					} else if (times1.equalsIgnoreCase("")
							|| times1.trim().length() == 0) {
						Toast.makeText(getBaseContext(), "How many Times ???",
								Toast.LENGTH_LONG).show();

					}

					else {
						SmsManager sms2 = SmsManager.getDefault();
						TelephonyManager telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
					     int simState = telMgr.getSimState();

						if(simState==TelephonyManager.SIM_STATE_ABSENT)
						{
							
							Toast.makeText(getBaseContext(),
									"Enter SIM", Toast.LENGTH_LONG)
									.show();

							
						}
						else
						{

						int j = Integer.parseInt(times1);
						

						if (j >= 200) 
								{

											Toast.makeText(getBaseContext(),
													"Enter Value < 200", Toast.LENGTH_SHORT)
													.show();

									} 
						else
						{
										for (int i = 1; i <= j; i++) 
										{
											sms2.sendTextMessage(phone1, null, sms1, null,
													null);
											Toast.makeText(getBaseContext(), ""+i,
													Toast.LENGTH_SHORT).show();

							}
										Toast.makeText(getBaseContext(), "Done!",
												Toast.LENGTH_SHORT).show();
							}
					}}

				} catch (Exception e)

				{
					Toast.makeText(getBaseContext(), e.toString(),
							Toast.LENGTH_LONG).show();
				}
			}
		});

	}
}